package com.mmd.manage.web;

import com.alibaba.fastjson.JSON;
import com.mmd.bean.CODE;
import com.mmd.bean.WebJsonBean;
import com.mmd.exception.GameException;
import com.mmd.intercepter.AdminInterceptor;
import com.mmd.manage.service.OrderService;
import com.mmd.manage.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 下单
 */
@RequestMapping(value = "/api/order")
@Controller
public class OrderController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/addOrder")
    @ResponseBody
    public WebJsonBean addOrder(OrderEo eo) {
        logger.info("入参=====>{}", JSON.toJSONString(eo));
        return new WebJsonBean(CODE.SUCCESS, orderService.addOrder(eo));
    }

    @RequestMapping(value = "/modifyOrder")
    @ResponseBody
    public WebJsonBean modifyOrder(OrderEo eo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.modifyOrder(eo));
    }

    @RequestMapping(value = "/myOrderList")
    @ResponseBody
    public WebJsonBean myOrderList(MyOrderQueryEo eo) {
        eo.setUid(AdminInterceptor.getUserLoginContext().getUid());
        return new WebJsonBean(CODE.SUCCESS, orderService.myOrderList(eo));
    }


    @RequestMapping(value = "/orderList")
    @ResponseBody
    public WebJsonBean orderList(MyOrderQueryEo eo) {
        eo.setUid(AdminInterceptor.getUserLoginContext().getUid());
        return new WebJsonBean(CODE.SUCCESS, orderService.orderList(eo));
    }

    @RequestMapping(value = "/batchDeleteOrder")
    @ResponseBody
    public WebJsonBean batchDeleteOrder(String orderNos) {
        if (StringUtils.isEmpty(orderNos)){
            throw new GameException(new CODE(9999, "请选择订单"));
        }
        List<String> orderNoList = Arrays.asList(orderNos.split(","));
        orderService.batchDeleteOrder(orderNoList);
        return new WebJsonBean(CODE.SUCCESS);
    }

    /**
     * 回传运单
     *
     * @param eo
     * @return
     */
    @RequestMapping(value = "/backLogistics")
    @ResponseBody
    public WebJsonBean backLogistics(BackLogisticsEo eo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.backLogistics(eo));
    }

    /**
     * 回传箱单
     *
     * @return
     */
    @RequestMapping(value = "/backBox")
    @ResponseBody
    public WebJsonBean backBox(OrderBoxDetailVo eo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.backBox(eo));
    }

    /**
     * 供应商-回传箱单
     *
     * @return
     */
    @RequestMapping(value = "/backSuBox")
    @ResponseBody
    public WebJsonBean backSuBox(SuOrderBoxDetailVo eo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.backSuBox(eo));
    }

    /**
     * 获取箱单最新报价
     *
     * @return
     */
    @RequestMapping(value = "/refreshBoxPrice")
    @ResponseBody
    public WebJsonBean refreshBoxPrice(OrderBoxDetailVo eo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.refreshBoxPrice(eo));
    }

    /**
     * 供应商-获取箱单最新报价
     *
     * @return
     */
    @RequestMapping(value = "/refreshSuBoxPrice")
    @ResponseBody
    public WebJsonBean refreshSuBoxPrice(SuOrderBoxDetailVo eo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.refreshSuBoxPrice(eo));
    }

    /**
     * 获取箱单详情
     *
     * @return
     */
    @RequestMapping(value = "/getBoxDetail")
    @ResponseBody
    public WebJsonBean getBoxDetail(String orderNo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.getBoxDetail(orderNo));
    }

    /**
     * 供应商-获取箱单详情
     *
     * @return
     */
    @RequestMapping(value = "/getSuBoxDetail")
    @ResponseBody
    public WebJsonBean getSuBoxDetail(String batchNo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.getSuBoxDetail(batchNo));
    }

    /**
     * 审核订单
     *
     * @return
     */
    @RequestMapping(value = "/auditOrder")
    @ResponseBody
    public WebJsonBean auditOrder(String orderNo, Integer flag) {
        return new WebJsonBean(CODE.SUCCESS, orderService.auditOrder(orderNo, flag));
    }

    /**
     * 获取物流商列表
     *
     * @return
     */
    @RequestMapping(value = "/getLogisticsList")
    @ResponseBody
    public WebJsonBean getLogisticsList() {
        return new WebJsonBean(CODE.SUCCESS, orderService.getLogisticsList());
    }


    /**
     * 获取订单详情
     *
     * @return
     */
    @RequestMapping(value = "/getOrderDetail")
    @ResponseBody
    public WebJsonBean getOrderDetail(String orderNo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.getOrderDetail(orderNo));
    }

    /**
     * 获取客户的装箱单信息
     *
     * @return
     */
    @RequestMapping(value = "/getCustomOrderBoxDetail")
    @ResponseBody
    public WebJsonBean getCustomOrderBoxDetail(String orderNo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.getCustomOrderBoxDetail(orderNo));
    }


    /**
     * 获取订单物流详情
     *
     * @return
     */
    @RequestMapping(value = "/getOrderLogisticsDetail")
    @ResponseBody
    public WebJsonBean getOrderLogisticsDetail(String orderNo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.getOrderLogisticsDetail(orderNo));
    }


    /**
     * 拉取物流详情
     *
     * @return
     */
    @RequestMapping(value = "/pullLogisticsInfo")
    @ResponseBody
    public WebJsonBean pullLogisticsInfo() {
        orderService.pullLogisticsInfo();
        return new WebJsonBean(CODE.SUCCESS, true);
    }

    /**
     * 合并订单
     *
     * @return
     */
    @RequestMapping(value = "/mergeOrder")
    @ResponseBody
    public WebJsonBean mergeOrder(String orderNos) {
        return new WebJsonBean(CODE.SUCCESS, orderService.mergeOrder(orderNos));
    }

    /**
     * 分单
     *
     * @return
     */
    @RequestMapping(value = "/splitOrder")
    @ResponseBody
    public WebJsonBean splitOrder(String pkgNos, String orderNo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.splitOrder(pkgNos, orderNo));
    }


    /**
     * 选择供应商
     *
     * @return
     */
    @RequestMapping(value = "/chooseSupplier")
    @ResponseBody
    public WebJsonBean chooseSupplier(String orderNo, String supplierCode, String routeNo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.chooseSupplier(orderNo, supplierCode, routeNo));
    }

    /**
     * 供应商订单列表
     *
     * @return
     */
    @RequestMapping(value = "/supplierOrderList")
    @ResponseBody
    public WebJsonBean supplierOrderList(SuOrderQueryEo eo) {
        long uid = AdminInterceptor.getUserLoginContext().getUid();
        eo.setUid(uid);
        return new WebJsonBean(CODE.SUCCESS, orderService.supplierOrderList(eo));
    }


    /**
     * admin订单页面-导出订单
     *
     * @param eo
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/exportAdminOrder")
    @ResponseBody
    public WebJsonBean exportAdminOrder(MyOrderQueryEo eo, HttpServletRequest request, HttpServletResponse response) {
        long uid = AdminInterceptor.getUserLoginContext().getUid();
        eo.setUid(uid);
        orderService.exportAdminOrder(eo, request, response);
        return new WebJsonBean(CODE.SUCCESS);
    }

    /**
     * 供应商订单页面-导出订单
     *
     * @param eo
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/exportOrder")
    @ResponseBody
    public WebJsonBean exportOrder(SuOrderQueryEo eo, HttpServletRequest request, HttpServletResponse response) {
        long uid = AdminInterceptor.getUserLoginContext().getUid();
        eo.setUid(uid);
        orderService.exportOrder(eo, request, response);
        return new WebJsonBean(CODE.SUCCESS);
    }

    /**
     * 供应商订单页面-下载转单模板
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/downloadTrackingTemplate")
    @ResponseBody
    public WebJsonBean downloadTrackingTemplate(HttpServletRequest request, HttpServletResponse response) {
        orderService.downloadTrackingTemplate(request, response);
        return new WebJsonBean(CODE.SUCCESS);
    }


    /**
     * 入仓
     *
     * @return
     */
    @RequestMapping(value = "/storage")
    @ResponseBody
    public WebJsonBean storage(String batchNo) {
        return new WebJsonBean(CODE.SUCCESS, orderService.storage(batchNo));
    }

    /**
     * 批量入仓
     *
     * @return
     */
    @RequestMapping(value = "/batchStorage")
    @ResponseBody
    public WebJsonBean batchStorage(String batchNos) {
        return new WebJsonBean(CODE.SUCCESS, orderService.batchStorage(batchNos));
    }

    /**
     * admin修改产品信息
     *
     * @return
     */
    @RequestMapping(value = "/adminModifyProduct")
    @ResponseBody
    public WebJsonBean adminModifyProduct(@RequestBody ModifyProductEo modifyProductEo) {
        orderService.adminModifyProduct(modifyProductEo.getParam());
        return new WebJsonBean(CODE.SUCCESS);
    }


    /**
     * 供应商订单页面-导入转单模板
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importTrackingFile")
    @ResponseBody
    public WebJsonBean importTrackingFile(HttpServletRequest request, HttpServletResponse response,
                                          @RequestParam("file") MultipartFile file) {

        List<ImportTrackingBean> list = new ArrayList<>();
        try {
            InputStream inputStream = file.getInputStream();
            XSSFWorkbook xWorkbook = new XSSFWorkbook(inputStream);
            XSSFSheet sheetAt = xWorkbook.getSheetAt(0);

            for (int i = 1; i < 2000; i++) {
                XSSFRow row = sheetAt.getRow(i);
                XSSFCell cell0 = row.getCell(0);
                cell0.setCellType(Cell.CELL_TYPE_STRING);
                String cell0Value = cell0.getStringCellValue();
                if (StringUtils.isBlank(cell0Value)) {
                    break;
                }
                XSSFCell cell1 = row.getCell(1);
                cell1.setCellType(Cell.CELL_TYPE_STRING);
                String cell1Value = cell1.getStringCellValue();

                XSSFCell cell2 = row.getCell(2);
                cell2.setCellType(Cell.CELL_TYPE_STRING);
                String cell2Value = cell2.getStringCellValue();
                ImportTrackingBean importTrackingBean = new ImportTrackingBean();
                importTrackingBean.setBatchNo(cell0Value);
                importTrackingBean.setTrackingNo(cell1Value);
                importTrackingBean.setLogisticsCompanyCode(cell2Value);

                list.add(importTrackingBean);
            }
            logger.info("###########{}", JSON.toJSONString(list));
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (!CollectionUtils.isEmpty(list)) {
            orderService.importTrackingFile(list);
        }
        return new WebJsonBean(CODE.SUCCESS);
    }


}
